$(function() {
	"use strict";
	
	var bindTabCloseClickEvent = function(){
		$("#index-nav").delegate(".tab-close","click",function(){
			var tab = $(this).closest(".tabbable");
			var nav = $(this).closest(".nav-tabs");
			var li = $(this).closest("li");
			var a = li.find('a');
			var contentId = a.attr('href');
			var show = li.next();
			
			li.remove();
			$(contentId).remove();
			
			if(show){
				show.find('a').click();
			}
		});
	}
	
	var bindMenuClickEvent = function(){
		$("#menu-ul").delegate(".nav-link","click",function(){
			var url = $(this).data("url");
			if(url){
				url = context+url;
				var id = 'menu'+$(this).data("menuId");
				
				var a = $("a[href='#"+id+"']");
				if(a.length>0){
					a.click();
					$(".nav-item","#menu-ul").removeClass("active open");
					
					$(this).parents(".nav-item").addClass("active open");
					return;
				}
				var menuName = $(this).data("menuName");
				var li ='<li>'+
                		'<a href="#'+id+'" data-toggle="tab">'+menuName+' <i class="fa fa-close tab-close"></i></a>'+
                		'</li>';
				$("#index-nav").append(li);
				var content = '<div class="tab-pane" id="'+id+'">'+
							  '<iframe src="'+url+'" frameborder=0 scrolling="yes" width="100%" height="100%"></iframe>'+
							  '</div>';
				$("#index-tab-content").append(content);
				a = $("a[href='#"+id+"']");
				a.click();
				
				$(".nav-item","#menu-ul").removeClass("active open");
				
				$(this).parents(".nav-item").addClass("active open");
			}
		});
	}
	
	var changeTabContentHeight = function(){
//		$("#index-tab").height($(".page-content").height());
		$("#index-tab-content").height($(".page-content").height()-$("#index-nav").height()-20);
	}
	
	var iterateMenu = function(menu){
		var html='';
		html = html+'<li class="nav-item">'+
		'<a href="javascript:;" class="nav-link" data-url="'+menu.menuUrl+'" data-menu-id="'+menu.menuId+'" data-menu-name="'+menu.menuName+'">'+
			'<i class="'+menu.menuIcon+'"></i>'+
			'<span class="title">'+menu.menuName+'</span>'+
		'</a>';
		if(menu.childList!=null&&menu.childList.length>0)
		{
		html = html+'<ul class="sub-menu">'
					for(var i=0;i<menu.childList.length;i++){
						html = html+iterateMenu(menu.childList[i]);
					}
		html = html+'</ul>'
		}
		html = html+   '</li>';
		return html;
	}
	
	var initIndexMenu = function(){
		var html = '';
		$.ajax({
            url:context+'/console/menu/session',
            type:'POST',
            dataType:'json',
            success:function(data,textStatus,jqXHR){
            	var html='';
                $.each(data,function(index,menu){
                	html = html + iterateMenu(menu);
                });
                
                $("#menu-ul").html(html);
                
            }
        });
	}
	
	var initModifyPassword = function(){
		var dialog = $("#modifyPasswordDialog");
		var form = $("#modifyPasswordForm",dialog);
		$(".modify_password").on('click',function(){
			dialog.modal();
		});
		
		$(".ok",dialog).on('click',function(){
			App.blockUI({
                target: '#modifyPasswordDialogContent',
                boxed: true,
                message: '数据处理中...',
                textOnly: true
            });
			
			$.ajax({
	            url:context+'/console/user/modifypassword',
	            type:'POST',
	            data:form.serialize(),
	            dataType:'json',
	            success:function(data,textStatus,jqXHR){
	            	App.unblockUI('#modifyPasswordDialogContent');
	                if(data.success){
	                	toastr["info"]("密码修改成功", "提示");
	                	dialog.modal('hide');
	                }else{
	                	toastr["error"](data.message, "提示");
	                }
	                
	            }
	        });
		});
		
	
	}

	var initUserCenter = function(){
		var dialog = $("#userCenterDialog");
		var form = $("#tenantForm",dialog);
		$(".user_center").on('click',function(){
			
			dialog.modal();
			
			if(form.length>0){
				form[0].reset();
				App.blockUI({
	                target: '#userCenterDialogContent',
	                boxed: true,
	                message: '数据加载中...'
	            });
				$.ajax({
		            url:context+'/app/tenant/session/',
		            type:'POST',
		            dataType:'json',
		            success:function(data,textStatus,jqXHR){
		            	App.unblockUI('#userCenterDialogContent');
		                if(data.success){
		                	iTsai.form.deserialize(form,data.data)
		                }else{
		                	toastr["error"]("加载商户数据异常", "提示");
		                }
		                
		            }
		        });
			}
			
		});
		
		$(".ok",dialog).on('click',function(){
			if(form.length>0){
				App.blockUI({
	                target: '#userCenterDialogContent',
	                boxed: true,
	                message: '数据处理中...',
	                textOnly: true
	            });
				
				$.ajax({
		            url:context+'/app/tenant/save',
		            type:'POST',
		            data:form.serialize(),
		            dataType:'json',
		            success:function(data,textStatus,jqXHR){
		            	App.unblockUI('#modifyPasswordDialogContent');
		                if(data.id){
		                	toastr["info"]("保存数据成功", "提示");
		                	dialog.modal('hide');
		                }else{
		                	toastr["error"](data.message, "提示");
		                }
		                
		            }
		        });
			}else{
				dialog.modal('hide');
			}
		});

		Dropzone.options.myDropzone = {
			url:context+'/console/user/avatar/upload',
			dictDefaultMessage: '<img alt="点击重新选择头像" id="avatarImg" class="img-circle" src="'+context+'/console/user/avatar" />',
			uploadMultiple:false,
			addRemoveLinks:false,
			previewTemplate: document.getElementById('preview-template').innerHTML,
			init: function() {
				this.on("complete", function(file) {
					
					$("#avatarImg").attr('src',context+'/console/user/avatar?'+new Date());
					$("#avatarImg1").attr('src',context+'/console/user/avatar?'+new Date());
					
					this.removeFile(file);
					
				});
			}            
		}
	
	
	}
	
	
	bindMenuClickEvent();
	bindTabCloseClickEvent();
	initIndexMenu();
	initModifyPassword();
	initUserCenter();
	
	window.onload = function(){
		changeTabContentHeight();
	}
	
	window.onresize=function(){  
	     changeTabContentHeight();  
	} 
	
});